package com.share.hystrix;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * author:caifan
 * date:2019/7/15
 */
public class CommandThatFailsSliently extends HystrixCommand<List<String>> {
    private final boolean throwException;

    public CommandThatFailsSliently(boolean throwException) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.throwException = throwException;
    }

    @Override
    protected List<String> run() {
        if (throwException) {
            throw new RuntimeException("fail from CommandThatFailSliently");
        } else {
            List<String> list = new ArrayList<>();
            list.add("success");
            return list;
        }
    }

    @Override
    public List<String> getFallback() {
        return Collections.emptyList();
    }
}
